<?php

class GoodsTypeModel extends Model{
	public $table='goods_type';
	public function addGoodsType($data){
		if(empty($data)){
			$this->error='请输入商品类型';
			return false;
		}
		$this->validate=array(
			array('gtname','nonull','商品名不能为空',2,1)
			);
		if($this->create($data)){
			$model = M('goods_type');
			if($model->where(array('gtname'=>$data['gtname']))->find()){
				$this->error='商品类型已经存在';
			}else{
				$state = $model->add();
				if($state){
					
					return true;
				}
			}
		}else{
			$this->error='商品类型不能为空';
		}
	}

	public function delGoodsType($id=null){
		if(is_null($id)){
			$this->error='无法删除，请选择要删除数据';
			return false;
		}else{
			if(M('goods_type')->delete('gt_id='.$id)){
				return true;
			}else{
				$this->error='删除失败';
				return false;
			}
		}
	}

	public function updateGoodsType($id){
		if(M('goods_type')->where(array('gt_id'=>$id))->save()){
			return true;
		}else{
			$this->error='更新失败';
			return false;
		}
	}

	public function dataCatch(){
		$allDataCatch = $this->all();
		$catchData=array();
		if(empty($allDataCatch)){
			return catched('goodsType',$catchData);
		}
		foreach ($allDataCatch as $d) {
			$catchData[$d['gt_id']]= $d;
		}

		return catched('goodsType',$catchData);
	}

}

?>
